<?php

namespace Common\Model;

/**
 * 权限验证
 * Class UserRuleModel
 * @package Common\Model
 */
class UserRuleModel extends BaseModel
{
    protected $_validate = array(
        array('name', '', '该URL规则已经存在！', 2, 'unique', self::MODEL_BOTH),
//        array('condition', 'require', '请填写对应规则！'),
    );
    protected $num2text = array(
        'type' => array(
            1 => '普通URL',
            2 => '系统菜单',
            3 => '系统栏目',
        ),
        'status'=>array(
            0=>'关闭',
            1=>'开启'
        )
    );
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME),
    );

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取类型规则
     * @param $type 1普通url;2菜单;3栏目/顶部导航
     * @param int $status
     * @return mixed
     */
    public function getRule($type, $status = 1, $map = array())
    {
        $m = array_merge(array(
            'status' => $status,
            'type' => $type,
        ), $map);
        $list = $this->where($m)->order('sort desc')->select();
        return $list;
    }

    /**
     * 获取模块规则
     * @param $module
     * @param int $status
     * @return mixed
     */
    public function getModuleRule($module, $type, $status = 1)
    {
        $map = array('module' => $module);
        $list = $this->getRule($type, $status, $map);
        $rules = D('Tree', 'Logic')->toTree($list);
        return $rules;
    }

    /**
     * 获取权限url
     * @param int $status
     * @return mixed
     */
    public function getUrl($status = 1)
    {
        $list = $this->getRule(1, $status);
        $urls = D('Tree', 'Logic')->toTree($list);
        return $urls;
    }

    /**
     * 获取菜单
     * @param int $status
     * @return mixed
     */
    public function getMenu($status = 1)
    {
        $list = $this->getRule(2, $status);
        $menu = D('Tree', 'Logic')->toTree($list);
        return $menu;
    }

    /**
     * 获取栏目/顶部导航
     * @param int $status
     * @return mixed
     */
    public function getChannel($status = 1)
    {
        $list = $this->getRule(3, $status);
        $menu = D('Tree', 'Logic')->toTree($list);
        return $menu;
    }

    /**
     * 获取模块菜单
     * @param $module
     * @param int $status
     */
    public function getModuleMenu($module, $status = 1)
    {
        $list = $this->getModuleRule($module, 2, $status);
        $menu = D('Tree', 'Logic')->toTree($list);
        return $menu;
    }

    /**
     * 获取模块栏目/顶部导航
     * @param $module
     * @param int $status
     */
    public function getModuleChannel($module, $status = 1)
    {
        $list = $this->getModuleRule($module, 3, $status);
        $channel = D('Tree', 'Logic')->toTree($list);
        return $channel;
    }

    protected function _after_find(&$result, $options)
    {
        parent::_after_find($result, $options);
        if ($result['icon']) {
            $result['icon_html'] = '<i class="' . $result['icon'] . '"></i>';
        }
        if(isset($result['pid'])) {
            $result['parent'] = $this->where(array('id'=>$result['pid']))->getField('title');
        }
        if(isset($result['name'])&&isset($result['function'])&&function_exists($result['function'])) {
            $result['url'] = $result['function']($result['name']);
        }
    }
}